package com.smartleanx.module.hrm.controller.admin.examine.examineDimension;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineDimension.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineDimension.ExamineDimensionDO;
import com.smartleanx.module.hrm.service.examine.examineDimension.ExamineDimensionService;

@Tag(name = "管理后台 - 考核维度")
@RestController
@RequestMapping("/hrm/examine-dimension")
@Validated
public class ExamineDimensionController {

    @Resource
    private ExamineDimensionService examineDimensionService;

    @PostMapping("/create")
    @Operation(summary = "创建考核维度")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:create')")
    public CommonResult<Integer> createExamineDimension(@Valid @RequestBody ExamineDimensionSaveReqVO createReqVO) {
        return success(examineDimensionService.createExamineDimension(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核维度")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:update')")
    public CommonResult<Boolean> updateExamineDimension(@Valid @RequestBody ExamineDimensionSaveReqVO updateReqVO) {
        examineDimensionService.updateExamineDimension(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核维度")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:delete')")
    public CommonResult<Boolean> deleteExamineDimension(@RequestParam("id") Integer id) {
        examineDimensionService.deleteExamineDimension(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核维度")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:query')")
    public CommonResult<ExamineDimensionRespVO> getExamineDimension(@RequestParam("id") Integer id) {
        ExamineDimensionDO examineDimension = examineDimensionService.getExamineDimension(id);
        return success(BeanUtils.toBean(examineDimension, ExamineDimensionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核维度分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:query')")
    public CommonResult<PageResult<ExamineDimensionRespVO>> getExamineDimensionPage(@Valid ExamineDimensionPageReqVO pageReqVO) {
        PageResult<ExamineDimensionDO> pageResult = examineDimensionService.getExamineDimensionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineDimensionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核维度 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-dimension:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineDimensionExcel(@Valid ExamineDimensionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineDimensionDO> list = examineDimensionService.getExamineDimensionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核维度.xlsx", "数据", ExamineDimensionRespVO.class,
                        BeanUtils.toBean(list, ExamineDimensionRespVO.class));
    }

}